package com.fingard.dsp.bank.directbank.citic02;

import com.fingard.dsp.bank.directbank.bankInterface.IUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.ReqQryUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/30.
 */
public class CITIC02QryUndueBillInfo extends CITIC02Base implements IUndueBillInfo {
    @Override
    public RetAtsUndueBillInfo qryUndueBillInfo(ReqQryUndueBillInfo recInfo) throws Exception {
        RetAtsUndueBillInfo retAts = new RetAtsUndueBillInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            CITIC02Header header = new CITIC02Header();
            header.stdprocode = "RLTMSG";
            header.msgsendertrc = reqHeader.transSeqID;
            header.trdcode = "CPCBUEPQ";
            //            封装body内容
            XmlTextWriter xmlWriterBody = new XmlTextWriter();
            xmlWriterBody.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlWriterBody.writeStartElement("stream");
//            CPCUSTCD	企业端经销商客户代码	VARCHAR	20	Y
            xmlWriterBody.writeElementString("CPCUSTCD",recInfo.DealerCode);
//            BKCUSTCD	银行端借款企业ID	VARCHAR	20	N
            xmlWriterBody.writeElementString("BKCUSTCD","");
//            LONCPNM	借款企业名称	VARCHAR	100	Y
            xmlWriterBody.writeElementString("LONCPNM",recInfo.DealerName);
//            OPBRHCD	经办分行编号	VARCHAR	10	N
            xmlWriterBody.writeElementString("OPBRHCD","");
//            OPBRHNM	经办分行名称	VARCHAR	100	N
            xmlWriterBody.writeElementString("OPBRHNM","");
//            CREDITNO	额度编号	VARCHAR	20	N
            xmlWriterBody.writeElementString("CREDITNO","");
//            FIELD1	属性1	VARCHAR	30	N
            xmlWriterBody.writeElementString("FIELD1","");
//            FIELD2	属性2	VARCHAR	60	N
            xmlWriterBody.writeElementString("FIELD2","");
//            FIELD3	属性3	VARCHAR	100	N
            xmlWriterBody.writeElementString("FIELD3","");
            xmlWriterBody.writeEndDocument();
            String[] tmpStrRet = sendToBank2(xmlWriterBody.toXmlString(), header);
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/stream/RETCODE");
                    if ("AAAAAAA".equals(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        while (tmpBkReader.read("/stream/list/row")) {
                            RetAtsUndueBillInfoDetail detail = new RetAtsUndueBillInfoDetail();
//                            BILNO	未结清银票票号	VARCHAR	30	Y
                            detail.BillNo = tmpBkReader.readedXElement.getSingleText("BILNO");
//                            BILAMT	未结清银票票面金额	VARCHAR	30	Y
                            detail.BillMoney = tmpBkReader.readedXElement.getSingleText("BILAMT");
//                            BILDT	未结清银票出票日期	VARCHAR	30	Y
                            detail.BillStartDate = tmpBkReader.readedXElement.getSingleText("BILDT");
//                            BILEDDT	未结清银票到期日	VARCHAR	30	Y
                            detail.BillEndDate = tmpBkReader.readedXElement.getSingleText("BILEDDT");
//                            FIRAMT	未结清银票对应首付保证金金额	VARCHAR	60	Y
                            detail.BailMoney = tmpBkReader.readedXElement.getSingleText("FIRAMT");
                            retAts.writeRetDetail(detail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetCode;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
